package domain;

import java.util.ArrayList;
import java.util.List;


public class table<T> extends element {

	List<T> list_object;
	
	
	
	List<ColumnBuilder<T>> list_columnBuilder = new ArrayList<ColumnBuilder<T>>();
	
	public table() {
	}
	public table(List<T> list) {
		init(list);
	}
	private void init(List<T> list){
		this.list_object = list;
		if(list.size()!=0){
			this.addAttribue(new AttributeProvider() {
				public String getValue() {
					return table.this.list_object.get(0).getClass().getSimpleName();
				}
				public String getName() {
					return "class";
				}
			});
		}
	}
	public void addColumn(ColumnBuilder<T> cb){
		this.list_columnBuilder.add(cb);
	}
	
	public void build(List<T> list){
		init(list);
		build();
	}
	
	public void build(){
		// build table head
		thead thead = new thead();
		tr tr_head = new tr();
		tr_head.addAttribue(new AttributeProvider() {
			
			@Override
			public String getValue() {
				// TODO Auto-generated method stub
				return "bg_black";
			}
			
			@Override
			public String getName() {
				// TODO Auto-generated method stub
				return "class";
			}
		});
		for(ColumnBuilder<T> cb : list_columnBuilder){
			tr_head.addChild(new th(cb.getTitle(list_object.size()!=0?list_object.get(0):null)));
		}
		this.addChild(thead.addChild(tr_head));
		
		// build table body
		for(T obj : list_object){
			tr tr = new tr();
			for(ColumnBuilder<T> cb : list_columnBuilder){
				tr.addChild(new td(cb.getValue(obj)));
			}
			this.addChild(tr);
		}
	}
	
	public static void main(String[] args) {
		List<Guy> list = new ArrayList<Guy>();
		list.add(new Guy());
		table<Guy> t = new table<Guy>();
		t.addColumn(new ColumnBuilder<Guy>() {
			
			@Override
			public String getValue(Guy t) {
				// TODO Auto-generated method stub
				return t.getName();
			}
			
			@Override
			public String getTitle(Guy t) {
				// TODO Auto-generated method stub
				return "Name";
			}
		});
		t.build(list);
		
		System.out.println(t);
		
	}
}
